package com.example.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.response.Result;
import com.example.product.entity.Product;
import com.example.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/{id}")
    public Result<Product> getProduct(@PathVariable Long id) {
        return Result.success(productService.getById(id));
    }

    @GetMapping("/category/{category}")
    public Result<Page<Product>> getProductsByCategory(
            @PathVariable String category,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return Result.success(productService.getProductsByCategory(category, page, size));
    }

    @PostMapping
    public Result<Boolean> createProduct(@RequestBody Product product) {
        return Result.success(productService.createProduct(product));
    }

    @PutMapping
    public Result<Boolean> updateProduct(@RequestBody Product product) {
        return Result.success(productService.updateProduct(product));
    }

    @PutMapping("/{id}/stock")
    public Result<Boolean> updateStock(@PathVariable Long id, @RequestParam Integer quantity) {
        return Result.success(productService.updateStock(id, quantity));
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProduct(@PathVariable Long id) {
        return Result.success(productService.deleteProduct(id));
    }
} 